# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=FreeImage

pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=3.16.0
pkgrel=2
pkgdesc="Library project for developers who would like to support popular graphics image formats (mingw-w64)."
arch=('any')
license=('GPL' 'custom:FIPL')
url="http://freeimage.sourceforge.net/"
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
        "${MINGW_PACKAGE_PREFIX}-libjpeg-turbo"
        "${MINGW_PACKAGE_PREFIX}-libpng"
        "${MINGW_PACKAGE_PREFIX}-libtiff"
        "${MINGW_PACKAGE_PREFIX}-LibRaw"
        "${MINGW_PACKAGE_PREFIX}-openjpeg2"
        "${MINGW_PACKAGE_PREFIX}-openexr")
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc")
options=('strip' 'staticlibs' '!buildflags')
source=("http://downloads.sourceforge.net/sourceforge/freeimage/FreeImage${pkgver//./}.zip"
        'FreeImage-3.16.0_mingw-makefiles.patch'
        'FreeImage-3.16.0_unbundle.patch'
        'FreeImage-3.16.0_disable-some-plugins.patch')
md5sums=('1a2d1fff6204adbd479cc98818892fc1'
         '20a0a9f7257df79bbcbdec62ed2446d9'
         '7be47d1342f5a8a59b9ec19495844661'
         'ffd4a31d2d1c2cf19af3a1e6232f8fd6')

prepare() {
  cd ${srcdir}/FreeImage

  # Convert to utf-8
  #iconv -f ISO-8859-15 -t UTF-8 Whatsnew.txt
  #iconv -f WINDOWS-1252 -t UTF-8 license-gplv3.txt
  #iconv -f ISO-8859-15 -t UTF-8 -o Whatsnew.txt.new Whatsnew.txt && \
  #  touch -r Whatsnew.txt Whatsnew.txt.new && \
  #  mv Whatsnew.txt.new Whatsnew.txt

  #iconv -f WINDOWS-1252 -t UTF-8 -o license-gplv3.txt.new license-gplv3.txt && \
  #  touch -r license-gplv3.txt license-gplv3.txt.new && \
  #  mv license-gplv3.txt.new license-gplv3.txt

  patch -p1 -i ${srcdir}/FreeImage-3.16.0_mingw-makefiles.patch
  patch -p1 -i ${srcdir}/FreeImage-3.16.0_unbundle.patch
  patch -p1 -i ${srcdir}/FreeImage-3.16.0_disable-some-plugins.patch

  # Remove bundled libraries
  rm -rf Source/OpenEXR
  rm -rf Source/LibMNG
  rm -rf Source/LibOpenJPEG
  rm -rf Source/LibPNG
  rm -rf Source/LibRawLite
  rm -rf Source/LibTIFF
  rm -rf Source/LibJPEG
  rm -rf Source/LibTIFF4
  rm -rf Source/ZLib
  rm -rf Source/LibWebP

  # Mingw-w64 has its own header
  rm -rf Source/LibJXR/common/include/guiddef.h

  #cp -f ${srcdir}/Makefile.mingw-w64 ${srcdir}/FreeImage/Makefile.mingw-w64
  
  # Generate source list
  sh ./gensrclist.sh
  sh ./genfipsrclist.sh
}

build() {
  cd ${srcdir}/FreeImage

  mkdir -p ${srcdir}/build-{shared,static}
  lndir ${srcdir}/FreeImage ${srcdir}/build-shared
  lndir ${srcdir}/FreeImage ${srcdir}/build-static

  plain "Build shared version..."
  cd ${srcdir}/build-shared
  make \
    MINGW_TARGET=${MINGW_CHOST} \
    FREEIMAGE_LIBRARY_TYPE="SHARED" \
    -f Makefile.mingw

  make \
    MINGW_TARGET=${MINGW_CHOST} \
    FREEIMAGE_LIBRARY_TYPE="SHARED" \
    -f Makefile.fip

  plain "Build static version..."
  cd ${srcdir}/build-static
  make \
    MINGW_TARGET=${MINGW_CHOST} \
    FREEIMAGE_LIBRARY_TYPE="STATIC" \
    -f Makefile.mingw

  make \
    MINGW_TARGET=${MINGW_CHOST} \
    FREEIMAGE_LIBRARY_TYPE="STATIC" \
    -f Makefile.fip
}

package() {
  install -d "${pkgdir}${MINGW_PREFIX}"/lib
  install -d "${pkgdir}${MINGW_PREFIX}"/bin
  install -d "${pkgdir}${MINGW_PREFIX}"/include
  install -d "${pkgdir}${MINGW_PREFIX}"/share/doc/FreeImage

  cd ${srcdir}/build-shared
  install -m755 Dist/libfreeimage-*.dll "${pkgdir}${MINGW_PREFIX}"/bin/
  install -m644 Dist/libfreeimage.dll.a "${pkgdir}${MINGW_PREFIX}"/lib/
  install -m755 Dist/libfreeimageplus-*.dll "${pkgdir}${MINGW_PREFIX}"/bin/
  install -m644 Dist/libfreeimageplus.dll.a "${pkgdir}${MINGW_PREFIX}"/lib/
  install -m644 Dist/FreeImage.h "${pkgdir}${MINGW_PREFIX}"/include/
  install -m644 Dist/FreeImagePlus.h "${pkgdir}${MINGW_PREFIX}"/include/
  
  install -m644 Whatsnew.txt "${pkgdir}${MINGW_PREFIX}"/share/doc/FreeImage/
  install -m644 license-fi.txt "${pkgdir}${MINGW_PREFIX}"/share/doc/FreeImage/
  install -m644 license-gplv2.txt "${pkgdir}${MINGW_PREFIX}"/share/doc/FreeImage/
  install -m644 license-gplv3.txt "${pkgdir}${MINGW_PREFIX}"/share/doc/FreeImage/

  cd ${srcdir}/build-static
  install -m644 Dist/libfreeimage.a "${pkgdir}${MINGW_PREFIX}"/lib/
  install -m644 Dist/libfreeimageplus.a "${pkgdir}${MINGW_PREFIX}"/lib/
}
